package com.hxkj.service.impl;

import com.github.pagehelper.PageInfo;
import com.hxkj.client.service.SystemMessageReceiveServiceClient;
import com.hxkj.client.service.UserInfoServiceClient;
import com.hxkj.domain.po.SysUser;
import com.hxkj.domain.po.SystemMessageReceive;
import com.hxkj.service.SystemMessageReceiveService;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.to.PageTo;
import com.platform.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
@Slf4j
public class SystemMessageReceiveServiceImpl implements SystemMessageReceiveService{

    @Autowired
    private SystemMessageReceiveServiceClient smrClient;
    @Autowired
    private UserInfoServiceClient userInfoServiceClient;

    @Override
    public PageInfo<SystemMessageReceive> getList(SystemMessageReceive systemMessageReceive, PageTo pageParam, HttpServletRequest request) throws Exception {
        String accessToken = request.getHeader("accessToken");

        if (org.apache.commons.lang3.StringUtils.isEmpty(accessToken))
            throw new CustomException("token为空");

        ResultDataStruct<SysUser> resultDataStruct = userInfoServiceClient.userinfoGet(accessToken);
        if (resultDataStruct.getCode() != 200)
            throw new CustomException(resultDataStruct.getMessage());

        SysUser user = resultDataStruct.getData();

        if (pageParam.getPageNum() == null || pageParam.getPageSize() == null) {
            pageParam.setPageNum(1);
            pageParam.setPageSize(10);
        }

        systemMessageReceive.setReceiverId(user.getUserId());
        if(StringUtils.isBlank(systemMessageReceive.getReceiverId())){
            throw new CustomMessageException("当前用户账号不能为空");
        }
        ParameterDataStruct<SystemMessageReceive> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setData(systemMessageReceive);
        parameterDataStruct.setPageTo(pageParam);
        return smrClient.getList(parameterDataStruct);
    }

    @Override
    public SystemMessageReceive getMessageInfo(SystemMessageReceive systemMessageReceive) throws Exception {
        if(systemMessageReceive.getId() == null){
            throw new CustomMessageException("消息编号不能为空");
        }
        ResultDataStruct<SystemMessageReceive> result = smrClient.getSystemMessageReceiveInfo(systemMessageReceive);
        if(result.getCode() != 200){
            throw new CustomMessageException(result.getMessage());
        }
        return result.getData();
    }

    @Override
    public void deleteSysMsg(String ids) throws Exception {
        if(StringUtils.isBlank(ids)){
            throw new CustomMessageException("消息编号不能为空！");
        }
        ResultMessageStruct result = smrClient.deleteReceiveMessage(ids);
        if(result.getCode() != 200){
            throw new CustomMessageException(result.getMessage());
        }
    }

    @Override
    public void updateMsgReadStatus(String ids) throws Exception {
        if(StringUtils.isBlank(ids)){
            throw new CustomMessageException("消息编号不能为空！");
        }
        ResultMessageStruct result = smrClient.updateMsgReadStatus(ids);
        if(result.getCode() != 200){
            throw new CustomMessageException(result.getMessage());
        }
    }
}
